package com.dream.inspect.modules.business.service.impl;

import com.dream.inspect.common.utils.StringUtil;
import com.dream.inspect.modules.business.dao.SuperviseQuestionDao;
import com.dream.inspect.modules.business.dao.SysXzqhDao;
import com.dream.inspect.modules.business.entity.SysXzqhEntity;
import com.dream.inspect.modules.business.service.HomeStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 首页统计图
 */
@Service("homeStatisticsService")
public class HomeStatisticsServiceImpl implements HomeStatisticsService {
    @Autowired
    private SuperviseQuestionDao superviseQuestionDao;
    @Autowired
    private SysXzqhDao sysXzqhDao;
    @Override
    public List<Map<String, Object>>  trendsView(Map<String, Object> params){
        if(StringUtil.isNotEmpty(params.get("xzqhId")+"")&&Long.valueOf(params.get("xzqhId")+"")>0){
            params.put("xzqhCode",getQhCode(Long.valueOf(params.get("xzqhId")+"")));
        }
        List<Map<String, Object>> countList= superviseQuestionDao.trendsView(params);
        Map<String, List<Map<String, Object>>> trendsViewMap=new HashMap<>();
        for (Map<String, Object> map : countList) {
            if(trendsViewMap.containsKey(map.get("yearDate")+"")){
                List<Map<String, Object>> maplist=  trendsViewMap.get(map.get("yearDate")+"");
                maplist.add(map);
                trendsViewMap.put(map.get("yearDate")+"",maplist);
            }else {
                List<Map<String, Object>> maplist=new ArrayList<Map<String, Object>>();
                maplist.add(map);
                trendsViewMap.put(map.get("yearDate")+"",maplist);

            }
        }
        List<Map<String, Object>> lsit=new ArrayList<Map<String, Object>>(10);
        for (String key : trendsViewMap.keySet()) {
            Map map=new HashMap();
            map.put("key",key);
            map.put("list",trendsViewMap.get(key));
            lsit.add(map);
        }
        return lsit;
        //return superviseQuestionDao.trendsView(parmaes);
    }
    @Override
    public List<Map<String, Object>>  problemChartAnnular(Map<String, Object> params){
        if(StringUtil.isNotEmpty(params.get("xzqhId")+"")&&Long.valueOf(params.get("xzqhId")+"")>0){
            params.put("xzqhCode",getQhCode(Long.valueOf(params.get("xzqhId")+"")));
        }
        List<Map<String, Object>> countList= superviseQuestionDao.problemChartAnnular(params);
        return countList;
        //return superviseQuestionDao.trendsView(parmaes);
    }
    public String getQhCode(Long xzqhId){
        SysXzqhEntity sysXzqhEntity = sysXzqhDao.selectById(xzqhId);
       if("0".equals(sysXzqhEntity.getLevel())){
           return sysXzqhEntity.getCode().substring(0,2);
       }
       if("1".equals(sysXzqhEntity.getLevel())){
           return sysXzqhEntity.getCode().substring(0,4);
       }
       if("2".equals(sysXzqhEntity.getLevel())){
           return sysXzqhEntity.getCode().substring(0,6);
       }
        return sysXzqhEntity.getCode();
    }


    @Override
    public List<Map<String, Object>>  rectifyChartSemiRing(Map<String, Object> params){
        if(StringUtil.isNotEmpty(params.get("xzqhId")+"")&&Long.valueOf(params.get("xzqhId")+"")>0){
            params.put("xzqhCode",getQhCode(Long.valueOf(params.get("xzqhId")+"")));
        }
        List<Map<String, Object>> countList= superviseQuestionDao.rectifyChartSemiRing(params);
        return countList;
        //return superviseQuestionDao.trendsView(parmaes);
    }
    @Override
    public List<Map<String, Object>>  yearDeptQuestionCount(Map<String, Object> params){
        if(StringUtil.isNotEmpty(params.get("xzqhId")+"")&&Long.valueOf(params.get("xzqhId")+"")>0){
            params.put("xzqhCode",getQhCode(Long.valueOf(params.get("xzqhId")+"")));
        }
        List<Map<String, Object>> countList= superviseQuestionDao.yearDeptQuestionCount(params);
        return countList;
        //return superviseQuestionDao.trendsView(parmaes);
    }
    @Override
    public List<Map<String, Object>>  findZoningQuestionStatisticsForApp(Map<String, Object> params){
        params.put("num",2);
        if("2".equals(params.get("level"))){
            List<Map<String, Object>> countList= superviseQuestionDao.findDeptQuestionStatisticsForApp(params);
            return countList;
        }
        if(StringUtil.isNotEmpty(params.get("xzqhId")+"")&&Long.valueOf(params.get("xzqhId")+"")>0){
            params.put("xzqhCode",getQhCode(Long.valueOf(params.get("xzqhId")+"")));
        }
        if("0".equals(params.get("level"))){
            params.put("num",4);
        }
        if("1".equals(params.get("level"))){
            params.put("num",6);

        }

        List<Map<String, Object>> countList= superviseQuestionDao.findZoningProvinceQuestionStatisticsForApp(params);
        // List<Map<String, Object>> countList= superviseQuestionDao.findZoningQuestionStatisticsForApp(params);
        return countList;
        //return superviseQuestionDao.trendsView(parmaes);
    }

}
